// 复制文本内容到剪贴板
function copyText(text) {
	var textarea = document.createElement("input");//创建input对象
	var currentFocus = document.activeElement;//当前获得焦点的元素
	document.body.appendChild(textarea);//添加元素
	textarea.value = text;
	textarea.focus();
	if(textarea.setSelectionRange)
		textarea.setSelectionRange(0, textarea.value.length);//获取光标起始位置到结束位置
	else
		textarea.select();
	try {
		var flag = document.execCommand("copy");//执行复制
	} catch(eo) {
		var flag = false;
	}
	document.body.removeChild(textarea);//删除元素
	currentFocus.focus();
	return flag;
}

// 选择元素内容
function SelectTextById(element) {
	var text = document.getElementById(element);
	var selection = window.getSelection();
	var range = document.createRange();
	range.selectNodeContents(text);
	selection.removeAllRanges();
	selection.addRange(range);
}

function SelectTextByJQSelector(selector) {
	if($(selector).is("input")) {
		$(selector).select();
	} else {
		var text = $(selector)[0];
		var selection = window.getSelection();
		var range = document.createRange();
		range.selectNodeContents(text);
		selection.removeAllRanges();
		selection.addRange(range);
	}
}

function InsertBtn(titleSelector, btnHandler, btnTitle) {
	var _insertBtn = document.createElement('span');
	_insertBtn.innerHTML = btnTitle;
	var _title = $(titleSelector)[0];
	_title.parentNode.insertBefore(_insertBtn, _title);

	var copyBtnStyle = "";
	copyBtnStyle += "margin-right: 10px;"
	copyBtnStyle += "cursor: pointer;";
	copyBtnStyle += "display: inline-block;";
	copyBtnStyle += "background: rgb(72, 72, 121);";
	copyBtnStyle += "color: rgb(255, 249, 0);";
	copyBtnStyle += "line-height: 28px;";
	copyBtnStyle += "border-radius: 4px;";
	copyBtnStyle += "box-sizing: border-box;";
	copyBtnStyle += "height: 32px;";
	copyBtnStyle += "font-size: 14px;";
	copyBtnStyle += "border: 1px solid #eaeaef;";
	copyBtnStyle += "text-align: center;";
	copyBtnStyle += "width: 80px;";
	_insertBtn.style = copyBtnStyle;

	_insertBtn.onclick = function(event) {
		btnHandler();
	}
}

function InsertSelectBtn(titleSelector, articleSelector, btnTitle) {
	InsertBtn(titleSelector, function() {
		SelectTextByJQSelector(articleSelector);
	}, btnTitle ? btnTitle : "选择内容");
}

function InsertZoomInBtn(titleSelector, btnHandler) {
	InsertBtn(titleSelector, function() {
		btnHandler();
	}, "窗口内全屏");
}

function InsertDownloadM3u8Btn(titleSelector, m3u8Url, fileName) {
	InsertBtn(titleSelector, function() {
		// 工具在 https://gitee.com/uid/m3u8-download-maker-for-ffmpeg
		var toolUrl = 'http://127.0.0.4:44444';
		var m3u8UrlBase64 = BASE64.urlsafe_encode(m3u8Url)
		var fileNameBase64 = BASE64.encode(fileName)
		// var makeDownUrl = toolUrl + "?m3u8=" + m3u8UrlBase64 + "&title=" + fileNameBase64
		$.ajax({
			type: "GET",
			url: toolUrl,
			data: {m3u8: m3u8UrlBase64, title: fileNameBase64},
			dataType: "text",
			success: function(data) {
				makeTips('执行结果: ' + data);
			}
		});
	}, "生成下载Bat");
}

function randomString(len) {
	len = len || 32;
	var t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
	a = t.length,
	n = "";
	for (i = 0; i < len; i++) n += t.charAt(Math.floor(Math.random() * a));
	return n
}

function makeTips(content, duration) {
	duration = duration || 3;
	var tipsId = 'm3u8Tips' + randomString();

	var tips = document.createElement('div');
	tips.innerHTML = content;
	tips.id = tipsId;

	var tipsStyle = "";
	tipsStyle += "position: fixed;"
	tipsStyle += 'z-index: 10000;';
	tipsStyle += 'right: 0px;';
	tipsStyle += 'padding: 10px;';
	tipsStyle += 'color: white;';
	tipsStyle += 'background-color: black;';
	tipsStyle += 'opacity: 0.3;';
	tips.style = tipsStyle;

	var _body = document.getElementsByTagName("body")[0];
	_body.parentNode.insertBefore(tips, _body);

	setTimeout(function() {
		var tipsElem = document.getElementById(tipsId);
		tipsElem.parentNode.removeChild(tipsElem);
	}, duration * 1000);
}
